<template>
  <el-dialog :visible="showRoleDialog" title="分配角色" @close="hClose">
    <div>
      <!-- 这里将来会放置多选列表 -->
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      {{ curId }}
      <div style="margin-top: 20px; text-align: right">
        <el-button type="primary" @click="hSure">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { getRolesEnabled } from '@/api/role'
import { getEmployeeDetail, assignRoles } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async hSure() {
      const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
      console.log(res)
      this.$message.success('保存成功')
      this.closeDialog()
    },
    async loadRoles() {
      const res = await getRolesEnabled()
      this.roleList = res
    },
    hClose() {
      this.$emit('update:showRoleDialog', false)
    },
    async getUserRole() {
      const res = await getEmployeeDetail(this.curId)
      console.log('获取当前员工的角色信息', res)
      this.roleIds = res.roleIds
    },
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>
